package com.adds.lvds.security.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adds.lvds.core.plugin.PageView;
import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.security.dao.SysResourcesMapper;
import com.adds.lvds.security.dao.SysRolesMapper;
import com.adds.lvds.security.dao.SysUserMapper;
import com.adds.lvds.security.model.SysRole;
import com.adds.lvds.security.model.SysRoleUser;

@Transactional
@Service("sysRoleService")
public class SysRoleService {
	@Autowired
	private SysRolesMapper roleMapper;
	@Autowired
	private SysResourcesMapper sysResourcesMapper;
	@Autowired
	private SysUserMapper sysUserMapper;

	/**
	 * 分页
	 * 
	 * @param pageView
	 * @param roles
	 * @return
	 */
	public PageView<SysRole> query(PageView<SysRole> pageView, SysRole roles) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("paging", pageView);
			map.put("t", roles);
			List<SysRole> list = roleMapper.pageSysRoleByCondition(map);
			pageView.setRecords(list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return pageView;
	}

	/**
	 * 查询角色表所有数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<SysRole> querySysRoleByMap(Map<String, Object> map) {
		List<SysRole> list = null;
		try {
			list = roleMapper.querySysRoleByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 根据条件获取角色数据
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public SysRole getSysRoleById(Map<String, Object> map) {
		SysRole sysRole = null;
		try {
			sysRole = roleMapper.getSysRoleById(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysRole;
	}

	/**
	 * 录入角色基本数据
	 * 
	 * @param sysRole
	 * @throws Exception
	 */
	public String addSysRole(SysRole sysRole) {
		String result = CommUtils.SUCC;
		try {
			roleMapper.addSysRole(sysRole);
		} catch (Exception e) {
			e.printStackTrace();
			result = CommUtils.ERROR;
		}
		return result;
	}

	/**
	 * 编辑角色基本数据
	 * 
	 * @param sysRole
	 * @throws Exception
	 */
	public String updateSysRole(SysRole sysRole) {
		String result = CommUtils.SUCC;
		try {
			roleMapper.updateSysRole(sysRole);
		} catch (Exception e) {
			e.printStackTrace();
			result = CommUtils.ERROR;
		}
		return result;
	}

	/**
	 * 批量删除角色数据
	 * 
	 * @param id
	 * @throws Exception
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String deleteSysRoleById(String ids) {
		String result = CommUtils.SUCC;
		try {
			roleMapper.deleteSysRoleById(ids.split(","));
			sysResourcesMapper.batchSysRoleRes(ids.split(","));
			sysUserMapper.batchSysUserRole(ids.split(","));
		} catch (Exception e) {
			result = CommUtils.ERROR;
			throw new RuntimeException();
		}
		return result;
	}

	public List<SysRole> findbyUserRole(String userId) {
		return roleMapper.findbySysUserRole(userId);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void addUserRole(SysRoleUser sysRoleUser) {
		try {
			roleMapper.deleteUserRole(sysRoleUser.getUserId().toString());
			roleMapper.addUserRole(sysRoleUser);
		} catch (Exception e) {
			throw new RuntimeException();
		}
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void addUserRole(String userId, List<String> ids) {
		roleMapper.deleteUserRole(userId);
		for (String roleId : ids) {
			if (!StringUtils.isEmpty(roleId)) {
				SysRoleUser sysRoleUser = new SysRoleUser();
				sysRoleUser.setUserId(Integer.parseInt(userId));
				sysRoleUser.setRoleId(Integer.parseInt(roleId));
				roleMapper.addUserRole(sysRoleUser);
			}
		}
	}

	public List<SysRole> findSysUserRoleByUserId(String id) {
		return roleMapper.findSysUserByUserId(id);
	}

	public List<SysRole> findNoSysUserRoleByUserId(String id) {
		return roleMapper.findNoSysUserByUserId(id);
	}

}
